package com.china08.yxyapi.api.manage.edu.platform.resource.yxylectureprepack;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.lecture.YxyLecturePrePack;
import com.china08.yxyapi.entity.mg.lecture.YxyLecturePrePack.Res;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLectureFeedBackRepository;
import com.china08.yxyapi.repository.mg.lecture.YxyLecturePrePackRepository;
import com.china08.yxyapi.security.profile.YxyProfile;
import com.china08.yxyapi.service.db.YxyLecturePrePackService;
import com.china08.yxyapi.util.DateToolUtils;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class YxyLecturePrePackFeedBackController extends YxyApiBaseController {

    Logger log = Logger.getLogger(YxyLecturePrePackStatusController.class);

    @Autowired
    YxyLectureFeedBackRepository yxyLectureFeedBackRepository;

    @Autowired
    YxyLecturePrePackService yxyLecturePrePackServiceImpl;

    @Autowired
    YxyLecturePrePackRepository yxyLecturePrePackRepository;

    @ApiOperation(value = "")
    @RequestMapping(value = "/api_yw/manage/edu/platform/resource/lectureprepack/feedback/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {

        String phaseId = reqModel.phaseId;
        String subjectId = reqModel.subjectId;
        String keyword = reqModel.keyword;

        YxyProfile yxyProfile = getProfile(request, response);

        // 当前学校
        String schoolId = yxyProfile.getSchoolId();
        String areaIdProvince = yxyProfile.getSchoolAreaIdProvince();
        String areaIdCity = yxyProfile.getSchoolAreaIdCity();
        String areaIdDistrict = yxyProfile.getSchoolAreaIdDistrict();
        Integer bureauLevel = yxyProfile.getSchoolBureauLevel();

        Assert.isTrue(StringUtils.isNotBlank(schoolId), "未获取到学校信息");
        Assert.isTrue(StringUtils.isNotBlank(areaIdProvince), "未获取到学校信息");
        Assert.isTrue(StringUtils.isNotBlank(areaIdCity), "未获取到学校信息");
        Assert.isTrue(StringUtils.isNotBlank(areaIdDistrict), "未获取到学校信息");
        Assert.isTrue(null != bureauLevel, "未获取到学校信息");

        // 云校schoolId
        // String yunSchoolId = "ff80808142a16af00142a1a60f010006";

        List<String> PrePackId = new ArrayList<String>();
        List<String> PrePackRed = new ArrayList<String>();
        long total = 0;

        // 当前用户是云校用户
        // if (schoolId.equals(yunSchoolId)) {
        // log.info("云校用户");
        // PrePackId = yxyLecturePrePackServiceImpl
        // .findPrePackIdForYunXiao(reqModel.page, reqModel.size);
        // total = yxyLecturePrePackServiceImpl
        // .countByPrePackIdAndStatusForYunXiao();
        // }
        // // 非云校用户
        // else {
        log.info("非云校用户");
        PrePackId = yxyLecturePrePackServiceImpl.findYxyLecturePrePackIdAndStatus(phaseId, subjectId, keyword, bureauLevel, areaIdProvince, areaIdCity, areaIdDistrict, 1, reqModel.order, reqModel.page, reqModel.size);

        // 模糊另一字段
        if (PrePackId.size() == 0) {
            PrePackId = yxyLecturePrePackServiceImpl.findYxyLecturePrePackIdAndStatus1(phaseId, subjectId, keyword, bureauLevel, areaIdProvince, areaIdCity, areaIdDistrict, 1, reqModel.page, reqModel.size);
        }
        // 小红点
        PrePackRed = yxyLecturePrePackServiceImpl.findErrerRedEdu(phaseId, subjectId, keyword, bureauLevel, areaIdProvince, areaIdCity, areaIdDistrict, 1);
        if (PrePackRed.size() == 0) {
            PrePackRed = yxyLecturePrePackServiceImpl.findErrerRedEdu1(phaseId, subjectId, keyword, bureauLevel, areaIdProvince, areaIdCity, areaIdDistrict, 1);
        }
        if (PrePackRed.size() != 0) {
            total = PrePackRed.size();
        } else {
            total = 0;
        }

        // }

        List<Pojo> list = new ArrayList<>();
        for (int i = 0; i < PrePackId.size(); i++)

        {
            String prePackId = PrePackId.get(i);
            YxyLecturePrePack yxyLecturePrePack = yxyLecturePrePackRepository.findById(prePackId);

            if (yxyLecturePrePack != null) {
                String id = yxyLecturePrePack.getId();
                String packName = yxyLecturePrePack.getPackName();
                String phaseName = yxyLecturePrePack.getPhaseName();
                String subjectName = yxyLecturePrePack.getSubjectName();
                String versionName = yxyLecturePrePack.getVersionName();
                String semesterName = yxyLecturePrePack.getSemesterName();
                String bookcontentsName = yxyLecturePrePack.getBookcontentsName();
                String userNick = yxyLecturePrePack.getUserNick();
                String schoolNick = yxyLecturePrePack.getSchoolNick();
                String auditUserName = yxyLecturePrePack.getAuditUserName();
                String auditSchoolName = yxyLecturePrePack.getAuditSchoolName();
                Date date = yxyLecturePrePack.getCreatedDate();
                List<Res> resources = yxyLecturePrePack.getResources();
                String createdDate = "";
                if (null != date) {
                    createdDate = DateToolUtils.format(date, "yyyy-MM-dd");
                }
                long feedCount = yxyLectureFeedBackRepository.countByPrepackId(prePackId);

                Pojo pojo = new Pojo();
                pojo.id = id;
                pojo.packName = packName;
                pojo.phaseName = phaseName;
                pojo.subjectName = subjectName;
                pojo.versionName = versionName;
                pojo.semesterName = semesterName;
                pojo.bookcontentsName = bookcontentsName;
                pojo.userNick = userNick;
                pojo.schoolNick = schoolNick;
                pojo.createdDate = createdDate;
                pojo.resources = resources;
                pojo.feedCount = feedCount;
                pojo.auditUserName = auditUserName;
                pojo.auditSchoolName = auditSchoolName;
                list.add(pojo);
            }
            // 举报表里面还有数据，而备课包里面已经没有数据了，
            // 手动减少备课包数量
            else {
                total = total - 1;
            }
        }

        RespModel respModel = new RespModel();
        respModel.list = list;
        respModel.total = total;
        return respModel;
    }

    public static class Pojo {
        public String id;
        public String packName;
        public String phaseName;
        public String subjectName;
        public String versionName;
        public String semesterName;
        public String bookcontentsName;
        public String userNick;
        public String schoolNick;
        public String createdDate;
        public List<Res> resources;
        public long feedCount;
        // 审核人
        public String auditUserName;
        // 审核人学校
        public String auditSchoolName;
    }

    protected static class ReqModel extends YxyApiPageReqModel {
        public String phaseId;
        public String subjectId;
        public String keyword;
        public Integer status;
        // 排序 正序：1 倒序：0 默认：0
        public Integer order;

    }

    protected static class RespModel extends YxyApiRespModel {
        public List<Pojo> list;
        public long total;
    }
}
